#include "Timer0.h"

void Timer0_init(void)									//configure this Timer to use the RealTimeCounter functionality
{
	/* Procedure to switch clock source from atmega128 datasheet on page 107 */
	TIMSK &= ~((1<< OCIE0) & (1<< TOIE0));            	//disable all interrupts for Timer0
	ASSR  = (1<<AS0);                                 	//Switch clock source to external
	TCCR0 = (0<<CS02) | (1<<CS01) | (0<<CS00);         	//Prescaler = 8 32768 / 8 = 4096
	while(ASSR & ((1<<TCN0UB) & (1<<OCR0UB) & (1<<TCR0UB)))// Are all asyncronous operations done?
		;
	TIFR &= ~((1<< OCF0) & (1<< TOV0));         		//clear interrupt flags for Timer0
	TIMSK |= (1<<OCIE0);                     //enable interrupt on overflow and compare match
}




